﻿using HCQ2_Common.Constant;
using HCQ2_Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace HCQ2WebAPI_Logic.BaseAPIController
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseAPI: BaseWeiXinApiLogic
    {
        /// <summary>
        /// 
        /// </summary>
        protected BaseAPI()
        {
            string userid = request[AuthorityConstant.USER_ID];
            userid = (string.IsNullOrEmpty(userid)) ? request.Headers[AuthorityConstant.USER_ID] : userid;
            userid = (string.IsNullOrEmpty(userid)) ? request.Form[AuthorityConstant.USER_ID] : userid;
            if (!string.IsNullOrEmpty(userid))
            {
                CurrentUser = getUser(userid);
            }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        protected T_User CurrentUser;

        /// <summary>
        /// 获取对应的用户
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected T_User getUser(string userid)
        {
            List<T_User> userList = operateContext.bllSession.T_User.Select(w => w.user_guid == userid);
            if (userList.Count == 0)
            {
                return null;
            }
            else
            {
                return userList[0];
            }
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="errmsg">返回的消息</param>
        /// <param name="value">返回的数据</param>
        /// <returns></returns>
        protected HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel ok(string errmsg, object value)
        {
            HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel jsonModel = new HCQ2_Model.ViewModel.WebAPI.
                WebApiResultJsonModel()
            {
                errcode = HCQ2_Common.Constant.WebResultCode.Ok,
                errmsg = errmsg,
                value = value
            };
            return jsonModel;
        }
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="value">返回的数据</param>
        /// <returns></returns>
        protected HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel ok(object value)
        {
            return ok("操作成功", value);
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <returns></returns>
        protected HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel ok()
        {
            return ok("操作成功", "");
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="errmsg">错误消息</param>
        /// <param name="value">错误数据源</param>
        /// <param name="errcode">错误代码</param>
        /// <returns></returns>
        protected HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel error(string errmsg, object value,int errcode= WebResultCode.Error)
        {
            HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel jsonModel = new HCQ2_Model.ViewModel.WebAPI.
                WebApiResultJsonModel()
            {
                errcode = errcode,
                errmsg = errmsg,
                value = value
            };
            return jsonModel;
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="value">返回数据源</param>
        /// <returns></returns>
        protected HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel error(object value)
        {
            return error("操作出现错误", value);
        }
        /// <summary>
        /// 返回失败
        /// </summary>
        /// <returns></returns>
        protected HCQ2_Model.ViewModel.WebAPI.WebApiResultJsonModel error()
        {
            return error("操作出现错误");
        }
    }
}
